/** <!--------------------------------------------------------------------------------------------->
 * @file RevModel.cpp
 *
 * GSvn - A Graphical Client for SVN
 * Copyright (C) 2012  Karl Krach, gsvn@bluespirit.la
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 <!---------------------------------------------------------------------------------------------> */

// -- includes --
#include "RevModel.h"
#include <QDebug>



/** <!--------------------------------------------------------------------------------------------->
 * Creates the RevModel object
 <!---------------------------------------------------------------------------------------------> */
RevModel::RevModel()
{
	//
	// Setup SVN Connection
	//
	if( !m_oConnection.IsLibraryCompatible() )
	{
		qWarning() << "SVN Library is not compatible!";
		return;
	}
	if( !m_oConnection.Initialize() )
	{
		qWarning() << "Initialization failed!";
		return;
	}
}	// END RevModel()

/** <!--------------------------------------------------------------------------------------------->
 * Deletes the RevModel object
 <!---------------------------------------------------------------------------------------------> */
RevModel::~RevModel()
{
}	// END ~RevModel()

/** <!--------------------------------------------------------------------------------------------->
 * Requests specified revisions and broadcasts them via SI_RevisionChanged()
 <!---------------------------------------------------------------------------------------------> */
void RevModel::SL_TriggerRevisionBroadcast( const SvnRevisionIdentifier& oRevID )
{
	// get root URL


	// set filter to path

	// request root URL
	m_oConnection.GetCompleteHistory( oRevID, this );
	emit SI_StatusChanged( "History Requested..." );

}	// END SL_TriggerRevisionBroadcast( const SvnRevisionIdentifier& )

/** <!--------------------------------------------------------------------------------------------->
 * Callback method to be called on each history element (triggered by 'svn log')
 *
 * @param iRevision     (in) SVN revision
 * @param oDateTime     (in) SVN commit time
 * @param strMessage    (in) SVN log message
 * @param strAuthor     (in) SVN author
 * @param aChangedFiles (in) changed files in this revision
 * @param aCopyInfo     (in) SVN copy info (branch info)
 <!---------------------------------------------------------------------------------------------> */
void RevModel::OnHistoryReceive( int iRevision, QDateTime oDateTime, QString strMessage,
                                 QString strAuthor, QStringList aChangedFiles,
                                 QList<SvnCopyInfo> aCopyInfo )
{
	emit SI_StatusChanged( QString("History Received: %1").arg(iRevision) );

	QString strMatchedBranch;
	bool bMatchedBranch = false;
	foreach( const QString& rFile, aChangedFiles )
	{
		if( m_oBranchPattern.indexIn(rFile) > -1 )
		{
			strMatchedBranch = m_oBranchPattern.cap(0);
			bMatchedBranch = true;
			break;
		}
	}

	if( !bMatchedBranch )
	{
		qWarning() << "XXXXXXXXXXXXXXX NO MATCH!! for " << aChangedFiles;
		return;
	}

	BranchIndex iBranchIndex = m_aBranches.indexOf( strMatchedBranch );
	if( iBranchIndex == -1 )
	{
		iBranchIndex = m_aBranches.size();
		m_aBranches.append( strMatchedBranch );

		// inform world of new branch
		emit SI_BranchChanged( iBranchIndex, strMatchedBranch );
	}

	QSP<RevModelItem> pItem( new RevModelItem() );

	pItem->m_iRevision = iRevision;
	pItem->m_oDateTime = oDateTime;
	pItem->m_aCopyInfo = aCopyInfo;
	pItem->m_strAuthor = strAuthor;
	pItem->m_strMessage = strMessage;
	pItem->m_iBranchIndex = iBranchIndex;

	emit SI_RevisionChanged(pItem);

}	// END OnHistoryReceive( int, QString, QString, QList<SvnCopyInfo> )

/** <!--------------------------------------------------------------------------------------------->
 * Callback method to be called on every progress-update
 *
 * @param fBytesReceived (in) Received bytes
 * @param fThrougput     (in) Throughput in kB/sec
 <!---------------------------------------------------------------------------------------------> */
void RevModel::OnHistoryProgress( float fBytesReceived, float fThrouput )
{
	Q_UNUSED(fBytesReceived);
	Q_UNUSED(fThrouput);

}	// END OnHistoryProgress( float, float )

/** <!--------------------------------------------------------------------------------------------->
 * Callback method to be called on every statistic update
 *
 * @param strCommand (in) Name of last command
 * @param iMsNeeded  (in) Milliseconds need for the last command
 * @param fBytesReceived (in) Received bytes
 <!---------------------------------------------------------------------------------------------> */
void RevModel::OnHistoryStatistics( QString strCommand, int iMsNeeded, float fBytesReceived )
{
	Q_UNUSED(strCommand);
	Q_UNUSED(iMsNeeded);
	Q_UNUSED(fBytesReceived);

}	// END OnHistoryStatistics( QString, int, float )

